/*
 * Copyright 2017-2021 Pranav Pandey
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pranavpandey.ohos.dynamic.toasts.internal;


import ohos.agp.components.Component;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.lang.reflect.Field;

/**
 * A Toast to fix the bad token exception on API 25.
 */
public final class ToastCompat extends ToastDialog {
    /**
     * Base toast used by this toast compat.
     */
    private final  ToastDialog mToast;

    public ToastCompat(Context context, ToastDialog base) {
        super(context);
        this.mToast = base;
        mToast.setTransparent(true);
    }

    /**
     * Make a standard toast that just contains a text view.
     *
     * @param context The context to use.
     * @param text The text to show. Can be formatted text.
     * @param duration The duration for the toast, either {@link ToastDialog#}
     *                 or {@link ToastDialog#}.
     *
     * @return The standard toast that just contains a text view.
     */
    public static ToastCompat makeText( Context context,CharSequence text, int duration) {
        ToastDialog toast = new ToastDialog(context).setText(String.valueOf(text)).setDuration(duration);
        setToastContext(toast.getComponent(), new ToastContext(context,toast));
        return new ToastCompat(context, toast);
    }

    /**
     * Make a standard toast that just contains a text view.
     *
     * @param context The context to use.
     * @param resId The resource id of the string resource to use. Can be formatted text.
     * @param duration The duration for the toast, either {@link ToastDialog#}
     *                 or {@link ToastDialog#}.
     *
     * @return The standard toast that just contains a text view.
     */
    public static ToastCompat makeText( Context context,  int resId, int duration)  {
        return makeText(context, context.getString(resId), duration);
    }

    /**
     * Sets the toast context to fix bad token exception.
     *
     * @param view The view used by the toast
     * @param context The context used by the toast.
     */
    private static void setToastContext(Component view, Context context) {
        if (view != null) {
            try {
                Field field = Component.class.getDeclaredField("mContext");
                field.setAccessible(true);
                field.set(view, context);
            } catch (IllegalAccessException |NoSuchFieldException e) {
                e.getMessage();
            }
        }
    }

    @Override
    public void show() {
        mToast.show();
    }

    @Override
    public ToastDialog setDuration(int ms) {
        return mToast.setDuration(ms);
    }


    @Override
    public ToastDialog setAlignment(int gravity) {
        return mToast.setAlignment(gravity);
    }


    @Override
    public ToastDialog setOffset(int offsetX, int offsetY) {
        return mToast.setOffset(offsetX, offsetY);
    }


    @Override
    public CommonDialog setTitleText(String text) {
        return mToast.setTitleText(text);
    }

    @Override
    public ToastDialog setSize(int width, int height) {
        return mToast.setSize(width, height);
    }

    @Override
    public ToastDialog setText(String text) {
        return mToast.setText(text);
    }

    @Override
    public ToastDialog setComponent(Component component) {
        return mToast.setComponent(component);
    }

    @Override
    public Component getComponent() {
        return mToast.getComponent();
    }

    /**
     * 返回toast实例
     *
     * @return toast
     */
    public  ToastDialog getToast() {
        return mToast;
    }
}
